//
//  SJPopoverAnimator.swift
//  SJWeiboSwift
//
//  Created by app;e on 15/10/4.
//  Copyright © 2015年 world. All rights reserved.
//  
//  负责动画的工具类

import UIKit

class SJPopoverAnimator: NSObject, UIViewControllerTransitioningDelegate,UIViewControllerAnimatedTransitioning {

    ///展现视图的大小
    var presentedFrame = CGRectZero
    
    ///标记 当前是否是modal状态
    var isPresented = false

    
    //返回负责转场的控制器对象
    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? {
       //实例化负责转场动画的对象
        let pc = SJPopoverPresentationController(presentedViewController: presented, presentingViewController: presenting)
        //设置展现视图的frame
        pc.presentedFrame = presentedFrame
        return pc
    }
    
    //返回提供 Modeal动画的 对象
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        //表示是modal
        isPresented = true
        return self
    }
    //返回提供 dismiss动画的 对象
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = false
        return self
    }
    
    //动画时长
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return isPresented ? 1.2 : 0.25
    }
    //转场动画实现函数 -  一旦实现此方法,默认的动画会失效, 所有的动画由程序员自己实现
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        //        let fromVC = transitionContext.viewControllerForKey(UITransitionContextFromViewKey)
        //        let toVC = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)
        
        
        if isPresented {
            //modal动画的时候展现的view 是toView
            let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!
            
            //1.将view添加容器视图
            transitionContext.containerView()?.addSubview(toView)
            
            //2.动画实现
            toView.transform = CGAffineTransformMakeScale(1, 0)
            toView.layer.anchorPoint = CGPoint(x: 0.5, y: 0)
            UIView.animateWithDuration(transitionDuration(transitionContext), delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
                toView.transform = CGAffineTransformIdentity
                }) { (_) -> Void in
                    //3.关闭动画,告诉系统动画已经完成, 不能少
                    transitionContext.completeTransition(true)
            }
        }else {
            //dismiss动画的时候展现的view 是fromView
            let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)
            
            UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
                
                //移除的时候scale的y值参数不能写成0.  只能写一个非常想的数字
                fromView?.transform = CGAffineTransformMakeScale(1, 0.001)
                }, completion: { (_) -> Void in
                    //将当前展现的view移除
                    fromView?.removeFromSuperview()
                    transitionContext.completeTransition(true)
                    
            })
            
            
        }
    }
    

}
